View Javadoc
1 2 package net.sf.voruta; 3 4 /*** 5 * 6 * @author baliuka 7 */ 8 final class SnapshotCache extends ReadCommitedCache{ 9 10 private static Object LOCK = new Object(); 11 12 13 /*** Creates a new instance of SerializbleCache */ 14 public SnapshotCache() { 15 } 16 17 public void put(Object key,Object value){ 18 19 super.put(key, new Cached(TxId.current().getId(),value)); 20 21 } 22 23 public Object get(Object key){ 24 25 Cached value = (Cached)getLocal().get(key); 26 if(value == null){ 27 synchronized(global){ 28 value = (Cached)global.get(key); 29 } 30 if( value != null ){ 31 32 if( value.getTxId() <= TxId.current().getLast() ){ 33 34 return value.getValue(); 35 }else{ 36 37 return null; 38 } 39 } 40 41 } 42 43 return value == null ? null : value.getValue(); 44 } 45 46 47 48 public void afterCommit() { 49 super.afterCommit(); 50 synchronized(LOCK){ 51 TxId.lastCommited = TxId.current().getId(); 52 } 53 54 TxId.clear(); 55 56 57 } 58 59 60 61 public void afterRollback() { 62 super.afterRollback(); 63 TxId.clear(); 64 } 65 66 public void beforeBegin() { 67 super.beforeBegin(); 68 TxId.clear(); 69 TxId.current(); 70 71 } 72 73 74 75 static class TxId { 76 77 private long id; 78 private long last; 79 private static long lastId = 0; 80 private static long lastCommited = 0; 81 82 private static ThreadLocal localTx = new ThreadLocal(); 83 84 private TxId(){ 85 synchronized(LOCK){ 86 last = lastCommited; 87 this.id = ++lastId; 88 89 } 90 } 91 92 static void clear(){ 93 localTx.set(null); 94 } 95 static TxId current(){ 96 TxId tid = (TxId)localTx.get(); 97 if(tid == null){ 98 tid = new TxId(); 99 localTx.set(tid); 100 } 101 return tid; 102 103 } 104 105 long getId(){ 106 return id; 107 } 108 long getLast(){ 109 return last; 110 } 111 112 } 113 114 static class Cached { 115 116 117 118 /*** Holds value of property txId. */ 119 private long txId; 120 121 /*** Holds value of property value. */ 122 private Object value; 123 124 public Cached(long txId, Object value) { 125 126 this.txId = txId; 127 this.value = value; 128 } 129 130 /*** Getter for property txId. 131 * @return Value of property txId. 132 */ 133 public long getTxId() { 134 return this.txId; 135 } 136 137 /*** Setter for property txId. 138 * @param txId New value of property txId. 139 */ 140 public void setTxId(long txId) { 141 this.txId = txId; 142 } 143 144 /*** Getter for property value. 145 * @return Value of property value. 146 */ 147 public Object getValue() { 148 return this.value; 149 } 150 151 /*** Setter for property value. 152 * @param value New value of property value. 153 */ 154 public void setValue(Object value) { 155 this.value = value; 156 } 157 158 } 159 160 161 }

This page was automatically generated by Maven